﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;
using Silvermoon.Images;
using OutlookHub.Models;
using OutlookHub.Styles;
using OutlookHub.Utils;

namespace OutlookHub.Pages
{
    public class CategoriesPage : Page
    {
        private ListView listView;
        private ListViewController<CheckItem, string> controller;

        private Dictionary<string, bool> selectedCategories = new Dictionary<string, bool>();

        public CategoriesPage()
            : base()
        {
            Styles = StylePool.TaskDetails;
            Title = "CHOOSE CATEGORIES";

            listView = new ListView { Margin = Margin.Zero, Multiselect = true };
            Controls.Add(listView);
            controller = new ListViewController<CheckItem, string>(listView);

            Toolbar tb = new Toolbar();

            tb.AddButton(SysImages.Ok).Tap += new EventHandler<TapEventArgs>(OnOk);
            tb.AddButton(SysImages.Add).Tap += new EventHandler<TapEventArgs>(OnNewCategory);
            tb.AddButton(SysImages.Cancel, Page.BackCommand);
            Toolbar = tb;

            InitController();
        }

        void OnNewCategory(object sender, TapEventArgs e)
        {
            //NotImplemented.Show();
            NewCategoryPage page = new NewCategoryPage { Category = string.Empty };
            KeepAlive = true;
            page.Hidden += (s, ee) => KeepAlive = false;
            page.OK += (s, ee) =>
                {
                    string category = page.Category;
                    OutlookContext.Context.AddCategory(category);
                    selectedCategories[category] = true;
                    int index = OutlookContext.Context.Categories.IndexOf(category);
                    controller.DataSource=OutlookContext.Context.Categories;
                    listView.BringItemIntoView(index, false);
                };
            this.NavigateTo(page);
        }

        void OnOk(object sender, EventArgs e)
        {
            if (Selected != null) Selected(this, EventArgs.Empty);
            GoBack();
        }

        public IEnumerable<string> SelectedCategories
        {
            get { return selectedCategories.Keys; }
            set
            {
                if (selectedCategories == null)
                {
                    selectedCategories = new Dictionary<string, bool>();
                }
                else
                {
                    selectedCategories = value.ToDictionary(c => c, v => true);
                }
                listView.UpdateItems();
            }
        }

        private void InitController()
        {
            controller.UpdateItem += new EventHandler<ItemControlGeneratorEventArgs<CheckItem>>(controller_UpdateItem);
            controller.InitializeItem += new EventHandler<ItemControlGeneratorEventArgs<CheckItem>>(controller_InitializeItem);
            controller.DataSource = OutlookContext.Context.Categories;
            controller.ValueComparer = (a, b) => string.Compare(a, b);
        }

        void controller_InitializeItem(object sender, ItemControlGeneratorEventArgs<CheckItem> e)
        {
            Item item = e.Item;
            item.IsHoldEnabled = true;
            item.SelectionChanged += new EventHandler(Item_SelectionChanged);
            item.Tap += new EventHandler<TapEventArgs>(OnItemTap);
            item.Hold += new EventHandler<TouchEventArgs>(OnItemHold);
        }

        void OnItemHold(object sender, TouchEventArgs e)
        {
            Item item = sender as Item;
            string category = item.DataValue as string;
            selectedCategories = new Dictionary<string, bool>(1);
            selectedCategories.Add(category, true);
            listView.UpdateItems();
        }

        void OnItemTap(object sender, TapEventArgs e)
        {
            Item item = sender as Item;
            string category = item.DataValue as string;
            selectedCategories = new Dictionary<string, bool>(1);
            selectedCategories.Add(category, true);
            listView.UpdateItems();
            OnOk(this, EventArgs.Empty);

        }

        void Item_SelectionChanged(object sender, EventArgs e)
        {
            Item item = sender as Item;
            string category = item.DataValue as string;
            if (item.IsSelected)
            {
                selectedCategories[category] = true;
            }
            else
            {
                selectedCategories.Remove(category);
            }
        }

        void controller_UpdateItem(object sender, ItemControlGeneratorEventArgs<CheckItem> e)
        {
            string category = e.Value as string;
            CheckItem item = e.Item;
            item.Text = category;
            item.IsSelected = selectedCategories.ContainsKey(category);

        }

        public event EventHandler Selected;
    }
}
